package com.lb.bs.client.processor.mgr;

import com.lb.bs.client.annotation.SingletonMark;
import com.lb.bs.client.processor.ProcessorHandleService;
import com.lb.bs.client.processor.ProcessorService;
import com.lb.bs.client.processor.beans.ProcessorBean;
import com.lb.bs.client.processor.beans.ProcessorDynamicBean;
import com.lb.bs.client.processor.enums.ProcessorType;
import com.lb.bs.client.util.CollectionUtils;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * program: LB-BS-Client
 * author: bsworld.xie
 * create: 2019-12-18 20:48
 * description:
 */
@SingletonMark
public class ProcessorCore<T> {

    private final Map<ProcessorType, ProcessorService> processorMap = new ConcurrentHashMap<>();

    private final Map<ProcessorType, List<? extends ProcessorHandleService>> processorFilterMap = new ConcurrentHashMap<>();


     void registerProcessorHandle(ProcessorType type, ProcessorService processorService) {
        processorMap.putIfAbsent(type, processorService);
    }

     void registerProcessorFilter(ProcessorType type,  List<? extends ProcessorHandleService> filterProcessors) {
        processorFilterMap.put(type, filterProcessors);
    }

     void dispatch(ProcessorBean<T> bean) {
        ProcessorType processorType = bean.getProcessorType();
         T t = bean.getT();
         ProcessorService processorService = processorMap.get(processorType);
        if (processorService == null) {
            return;
        }

        processorService.process(t);
    }

}
